/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef POSTEDIT_H
#define POSTEDIT_H

#include <QtGui/QDialog>
#include <QGraphicsScene>
#include <QModelIndex>
#include <QSharedPointer>
#include <QTemporaryFile>
#include "keymap.h"
#include "components/chan.h"

namespace Ui
{
	class PostEdit;
}

namespace Wishmaster
{
	class Chan;
	class ChanList;
	class Thread;
	struct PostDescriptor;
}

class ImageEditor;
class SpellCheckHighlighter;

using Wishmaster::ThreadId; // fucking moc
class PostEdit : public QDialog
{
	Q_OBJECT
public:
	PostEdit(Wishmaster::ChanManager* chanManager, const Wishmaster::ThreadId& threadId, QWidget *parent = 0);
	virtual ~PostEdit();

	void setText(const QString& txt);

public slots:
	void captcha(const ThreadId& threadId, const QByteArray& p);
	void send();
	void browse();
	void selectFromImageManager();
	void pictureSelected(const QString& path);
	void refreshCaptcha();
	void postingError(const QString& errortype);
	void setDetailsVisibility(bool v);
	void updateThreadOverview();
	void setThreadOverviewVisible(bool v);
	void editImage();
	void attachSelectionChanged();
	void attachDelete();
	void textChanged();
	void showFeaturesDialog();

protected slots:
	void threadOverviewClicked(const QModelIndex& index);
	void imageReady(QSharedPointer<QTemporaryFile> file);

protected:
    void updateAttachments();
	void changeEvent(QEvent *e);
	virtual void timerEvent(QTimerEvent* e);
	bool eventFilter(QObject* obj, QEvent* event);
	void addAttachments(Wishmaster::PostDescriptor* desc);
	void fillAutoAttachments();
    QPixmap pixmapForFile(const QString& filename);
	void appendAddendum(Wishmaster::AttachmentDescriptor& attach);

private:
	void updatePicLimits();
	void updateMessages();
	void updateGui();
	void initUi();

	void preprocessPost(Wishmaster::PostDescriptor& post);

	Ui::PostEdit *m_ui;
	Wishmaster::ChanManager* m_chanManager;
	Wishmaster::ThreadId m_threadId;
	bool m_eventLock; //Hack
	int m_timerID;
	QPixmap m_captcha;
	QGraphicsScene m_scene;
	Wishmaster::Keymap* m_keymap;
	bool m_threadOverviewVisible;
	QList<QSharedPointer<QTemporaryFile> > m_tempfiles;
	QList<Wishmaster::AttachmentDescriptor> m_attachments;
	ImageEditor* m_imageEditor;
	Wishmaster::BoardConstraints m_constraints;

	static const int PostIdRole = Qt::UserRole + 2;

	SpellCheckHighlighter* m_highlighter;

	bool m_charLimit;
	bool m_picLowLimit;
	QStringList m_ratings;
};

#endif // POSTEDIT_H

